    // 页面初始化时，加载表格
    $(function () {
        var optionstring = "";
        for (var i = 0; i < 5; i++) {
            optionstring += "<tr id="+i+">\n" +
                "<td><input type=\"checkbox\" class=\"select\" disabled='disabled' id='choice"+i+"' name=\"checkGood\" onclick=\"choose(this,"+i+")\"></td>\n" +
                "<td>\n" +
                "<div><input class=\"input-text radius\" id=\"skuCode"+i+"\" oninput=\"getGood(event,"+i+")\" placeholder=\"请输入商品编号或助记码\" type=\"text\">\n" +
                "<ul class=\"relation radius guanlian\" id=\"tuan"+i+"\" ><li>无数据</li></ul></div>\n" +
                "</td>\n" +
                "<td id=\"sku_name"+i+"\"></td>\n" +
                "<td id=\"tradePrice"+i+"\"></td>\n" +
                "<td id=\"discountPrice"+i+"\"></td>\n" +
                "<td><div id=\"count"+i+"\"></div></td>\n" +
                "<td><div><span id=\"actual_stock"+i+"\"></span></div></td>\n" +
                "<td><span id=\"total"+i+"\"></span></td>\n" +
                "<td><a onclick='delGood("+i+")'>删除</a></td>\n" +
                "</tr>";
        }
        $("#goodsTable").html(optionstring);

        // 添加商品回显
        if (localStorage.getItem("skuGoodList") != null) {
            var sku = JSON.parse(localStorage.getItem("skuGoodList"));
            var cus = JSON.parse(localStorage.getItem("customer"));
            // console.log("------------"+sku.length)
            for (var i = 0; i < sku.length-1; i ++) {
                console.log("------------"+i);

                var data = {
                    skuCode: sku[i].sku_code,
                    customerId: cus.customer_id,
                    cityId: 1
                };

                var j = 0;
                var succCallBack = function (data, status, response) {
                    if (data.errorCode == "0") {
                        var jsonObj = data.data;
                        console.log(jsonObj);

                        $("#choice"+j).attr('checked', "checked");
                        $("#choice"+j).removeAttr('disabled');
                        $("#skuCode"+j).val(jsonObj[0].sku_code).text(jsonObj[0].sku_id+","+jsonObj[0].spu_id);
                        $("#sku_name"+j).html("<img src='"+jsonObj[0].imgUrl+"' width='30px' height='30px'/>"+jsonObj[0].sku_name);
                        $("#tradePrice"+j).html(jsonObj[0].price+"元/"+jsonObj[0].unit);
                        $("#actual_stock"+j).html(jsonObj[0].actual_stock+"库存");
                        if (jsonObj[0].discountPrice == null) {
                            $("#discountPrice"+j).html("");
                        } else {
                            $("#discountPrice"+j).html(jsonObj[0].discountPrice+"元/"+jsonObj[0].unit);
                        }
                        $("#count"+j).html("<input class=\"input-text radius\" onblur=\"num_price("+j+")\" value='"+sku[j].buyyer_count+"' id=\"buyyerCount"+j+"\" type=\"text\">");
                        num = parseFloat(sku[sku.length-1]);
                        // console.log(parseFloat(sku[sku.length-1]))
                        num_price(j);
                        isChecked();
                        choose(this, j)
                        // setTotalPrice(num)
                    }
                    j++;

                };
                web.doAjax(root.congood.custSkuDetailList, 'post', data, succCallBack);
            }
        }

    });

    // 根据输入的sku码查询商品信息列表
    function getGood(e, Id) {
        //sku码输入框获取光标

        var skuCode = e.target.value;
        var cus = JSON.parse(localStorage.getItem("customer"));
        // console.log(cus);
        var goods_data = {
            skuCode: skuCode,
            customerId: cus.customer_id,
            cityId: 1
        };

        var succCallBack = function (data, status, response) {
            console.log(data);
            if (data.errorCode == "0") {
                var jsonObj = data.data;
                if (jsonObj == "") {
                    layer.msg('无此商品', {icon: 6,time:1500});
                    $("#tuan"+Id).html("<li>没有商品</li>");
                } else {
                    console.log(jsonObj);
                    if (jsonObj == "" || jsonObj == null) {
                        // hui.alert('无此商品','好的',function () {console.log('ok')})
                        $("#tuan"+Id).html("<li>没有商品</li>");
                    } else {

                        var optionstring = "";
                        for (var i = 0; i < jsonObj.length; i++) {
                            var json = JSON.stringify(jsonObj[i]);
                            optionstring += "<li onclick='relation(\""+Id+"\",\""+jsonObj[i].sku_code+"\","+json+",\""+jsonObj[i].sku_id+"\",\""+jsonObj[i].spu_id+"\")'>" + jsonObj[i].sku_name + "</li>";
                        }
                        $("#tuan"+Id).html(optionstring);
                    }
                }
            } else {
                // 弹框提示，无此商品
                layer.msg('无此商品', {icon: 6,time:1500});
                $("#tuan"+Id).html("<li>没有商品</li>");
            }
        };
        web.doAjax(root.congood.custSkuDetailList, 'post', goods_data, succCallBack);
    }

    // sku码输入框获取光标
    $(document).bind("click", function (e) {
        var tr = e.target.id;
        var id = tr.substring(tr.length-1);

        $("#tuan"+id).show();

        if ($(e.target).closest("#tuan"+id).length == 0 && $(e.target).closest("#skuCode"+id).length == 0) {
            $("#tuan"+id).hide()
        }

    });

    // 商品名称点击后触发的一个方法
    function relation(num, skucode, obj, skuid, spuid) {
        console.log(obj);

        if (obj != "" && obj.actual_stock > 0) {
            $("#choice"+num).attr('disabled', false);
            $("#skuCode"+num).val(skucode).text(skuid+","+spuid);
            $("#tuan"+num).hide();

            $("#sku_name"+num).html("<img src='"+obj.imgUrl+"' width='30px' height='30px'/>"+obj.sku_name);
            $("#tradePrice"+num).html(obj.price+"元/"+obj.unit);
            $("#actual_stock"+num).html(obj.actual_stock+"库存");
            if (obj.discountPrice == null) {
                $("#discountPrice"+num).html("");
            } else {
                $("#discountPrice"+num).html(obj.discountPrice+"元/"+obj.unit);
            }
            $("#count"+num).html("<input class=\"input-text radius\" onblur=\"num_price("+num+")\" value='1' id=\"buyyerCount"+num+"\" type=\"text\">");
            num_price(num)
        } else {
            // 库存不足，弹框提示
            layer.msg('该商品缺货，不能添加', {icon: 6,time:1500});
        }
        isChecked();
    }

    // 选择计算总价
    var num = 0;
    function choose(t, id) {
        isChecked();
        var total_price = getNumber($("#total"+id).html(), "元");
        if (total_price != "") {
            if (t.checked) {
                num += parseFloat(total_price);
                setTotalPrice(num);
            } else {
                num -= parseFloat(total_price);
                setTotalPrice(num);
            }
        }

    }

    function isChecked() {
        var sellAll = document.getElementById("sellAll");
        var all = document.getElementsByName("checkGood");
        var count = 0;
        var s = 0;
        for (var i = 0; i < all.length; i++) {
            var obj = all[i];
            if (obj.getAttribute('disabled') == null) {
                s++;
                if (obj.checked == true) {
                    count++;
                }
                if (count == s) {
                    sellAll.checked = true;
                } else {
                    sellAll.checked = false;
                }
            }
        }
    }

    // 数量判断
    function num_price(Id) {
        var count = parseFloat($("#buyyerCount"+Id).val());// 数量

        var reg= /^[0-9]*$/;
        if (count == "") {
            layer.msg('不能为空，请输入', {icon: 6,time:1500});
            $("#buyyerCount"+Id).val(1);
        } else if (!reg.test(count)) {
            layer.msg('格式错误，请重新输入', {icon: 6,time:1500});
            $("#buyyerCount"+Id).val(1);
        }

        var stock = getNumber($("#actual_stock"+Id).html(), "库");// 库存.num
        var price = $("#discountPrice"+Id).html();// 折扣价.num
        if (price == "") {
            price = $("#tradePrice"+Id).html();
        }
        price = getNumber(price, "元");
        var total = getNumber($("#total"+Id).html(), "元");// 选中，并修改数量之前的小计.num

        var choice = document.getElementById("choice"+Id);
        if (choice.checked) {// 判断是否被选中
            console.log("选中")

            if (count != 0 && count > stock) {// 判断数量值是否大于库存
                layer.msg('超出现有库存，不允许', {icon: 6,time:1500});
                $("#buyyerCount"+Id).val(1);

                var totalprice = price * 1;
                $("#total"+Id).html(totalprice+"元");
                num -= total;
                num += totalprice;
                setTotalPrice(num);
            } else {// 不大于库存，就结算小计并修改总计
                // if (total == 0) {
                //     var totalprice = price * count;
                //     console.log(total_price);
                //     console.log(num)
                //     $("#total"+Id).html(totalprice+"元");
                //     setTotalPrice(num);
                // } else {
                    var totalprice = price * count;
                    $("#total"+Id).html(totalprice+"元");
                    num -= total;
                    num += totalprice;
                    setTotalPrice(num);
                // }
            }
        } else {// 判断数量值是否大于库存
            if (count != "" && count > stock) {
                layer.msg('超出现有库存，不允许', {icon: 6,time:1500});
                $("#buyyerCount"+Id).val(1);
                var totalprice = price * 1;
                $("#total"+Id).html(totalprice+"元");
            } else {
                // 不大于库存，就结算小计
                var totalprice = price * count;
                $("#total"+Id).html(totalprice+"元");
            }
        }


    }

    // 删除
    function delGood(Id) {
        var total = getNumber($("#total"+Id).html(), "元");// 小计
        if ($("#choice"+Id).is(':checked')) {// 如果当前行被选中，就从总价中减去
            num -= total;
            setTotalPrice(num);
        }
        // isChecked();

        $("#"+Id).html(
            "<td><input type=\"checkbox\" class=\"select\" disabled='disabled' id='choice"+Id+"' name=\"checkGood\" onclick=\"choose(this,"+Id+")\"></td>\n" +
            "<td>\n" +
            "<div><input class=\"input-text radius\" id=\"skuCode"+Id+"\" oninput=\"getGood(event,"+Id+")\" placeholder=\"请输入商品编号或助记码\" type=\"text\">\n" +
            "<ul class=\"relation radius guanlian\" id=\"tuan"+Id+"\" ><li>无数据</li></ul></div>\n" +
            "</td>\n" +
            "<td id=\"sku_name"+Id+"\"></td>\n" +
            "<td id=\"tradePrice"+Id+"\"></td>\n" +
            "<td id=\"discountPrice"+Id+"\"></td>\n" +
            "<td><div id=\"count"+Id+"\"></div></td>\n" +
            "<td><div><span id=\"actual_stock"+Id+"\"></span></div></td>\n" +
            "<td><span id=\"total"+Id+"\"></span></td>\n" +
            "<td><a onclick='delGood("+Id+")'>删除</a></td>\n"
            )
    }

    function deleteAll() {
        var all = document.getElementsByName("checkGood");
        for (var i = 0; i < all.length; i++) {
            var obj = all[i];
            if (obj.checked) {
                var s = obj.getAttribute('id');
                var id = s.substring(s.length-1);
                delGood(id)
                // console.log($('#sellAll').is(':checked'));
                if ($('#sellAll').is(':checked')) {
                    $('#sellAll').attr('checked', false)
                }
            }
        }
    }

    // 全选
    function run() {
        var sellAll = document.getElementById("sellAll");
        var all = document.getElementsByName("checkGood");
        var totalPrice = 0;
        if (sellAll.checked == true) {
            for (var i = 0; i < all.length; i++) {
                var obj = all[i];
                if (obj.getAttribute("disabled")) {
                    console.log(obj.getAttribute("disabled"));
                    obj.checked = false;
                } else {
                    obj.checked = true;
                    var id = obj.getAttribute('id');
                    var strid = id.substring(id.length-1);
                    // console.log(strid);
                    var total = $("#total"+strid).html();
                    total = total.substring(0, total.length-1);// 小计
                    totalPrice += parseFloat(total);
                }

            }
            num = totalPrice;
            setTotalPrice(num)
        } else {
            for (var i = 0; i < all.length; i++) {
                var obj = all[i];
                obj.checked = false;
            }
            num = 0;
            setTotalPrice(num)
        }


    }

    function setTotalPrice(num) {
        $(".price").html("商品金额总计：<span>¥ "+num+"</span>");
    }

    // 获取价格
    function getNumber(pri, unit) {
        if (pri != "") {
            var price = pri.substring(0, pri.indexOf(unit));
            return parseFloat(price)
        }
        return 0
    }

    //提交订单
    function submitorder() {
        var orderList = {};
        orderList.spuSkuModelReqList = [];// spu集合

        var customer = window.localStorage.getItem("customer");// 客户信息
        var address = window.localStorage.getItem("address");// 收货地址
        var cus = JSON.parse(customer);// 转对象
        var add = JSON.parse(address);// 转对象

        orderList.memberId = cus.member_id;
        orderList.customerCode = cus.customer_code;
        orderList.customerId = cus.customer_id;
        orderList.cityId = 1;

        // var shippingAddressModel = {};
        // 收货地址
        orderList.shippingAddressModel = {
            addressAreaCode: "",
            receivedName: add.linkman,
            receivedPhone: add.mobile,
            receiverArea: add.district_id,
            receiverAreaName: add.areaName,
            receiverCity: add.city_id,
            receiverCityName: add.cityName,
            receiverDetailAddress: add.detail_address,
            receiverProvince: add.province_id,
            receiverProvinceName: add.provinceName,
            receiverTown: 0,
            receiverTownName: "",
            customerEmail: "",
            customerLandinePhone: "",
            receiverDetailAddressAlias: add.add_alias,
            shipAddId: add.ship_add_id
        };
        // console.log(orderList);


        var skuGoodList = [];
        $("input:checkbox[name='checkGood']:checked").each(function(){

            var Id = $(this).parents('tr').attr("id");
            // console.log(Id);
            var sku_code = $("#skuCode"+Id).val();
            var skuOneGood = {};
            skuOneGood.sku_code = sku_code;

            var sku_spu = $("#skuCode"+Id).text();
            var split = sku_spu.split(",");
            var sku_id = split[0];
            var spu_id = split[1];
            var buyyer_count = $("#buyyerCount"+Id).val();
            skuOneGood.buyyer_count = buyyer_count;
            skuOneGood.lineId = Id;
            skuGoodList.push(skuOneGood);


            var product_pic = $("#sku_name"+Id).children().attr("src");
            var price = $("#discountPrice"+Id).html();// 折扣价.num
            if (price == "") {
                price = $("#tradePrice"+Id).html();
            }
            var sale_price = getNumber(price, "元");

            var spuOneData = {};
            var skuModelReqList = [];// sku集合
            var orderData = {};

            orderData = {
                buyyerCount: buyyer_count,
                cartId: 69,
                memberId: cus.member_id,
                productPic: product_pic,
                salePrice: sale_price,
                skuId: sku_id,
                skuInfo: "",
                spuId: spu_id,
                customerId: cus.customer_id
            }

            spuOneData.spuName = "";
            skuModelReqList.push(orderData);
            // console.log(JSON.stringify(skuModelReqList));

            spuOneData.skuModelReqList = skuModelReqList;

            orderList.spuSkuModelReqList.push(spuOneData);
        });

        // 存总价，用于回显
        var money = $(".price").children().html();
        money = money.substring(money.indexOf(" ")+1);
        skuGoodList.push(money);

        // localStorage.setItem("orderList", JSON.stringify(orderList));
        localStorage.setItem("skuGoodList", JSON.stringify(skuGoodList));// skuCode、buyyerCount、money，用于回显

        var condeta=JSON.stringify(orderList);
        console.log(condeta)
        $.ajax({
            type:'post',
            /*'http://192.168.0.107:7070/trade/goTradePageNew'*/
            url:root.orderInter.goTradePageNew,
            dataType:'json',
            data:condeta,//需要将当前的对象转换为json字符串,
            contentType : 'application/json',
            success: function(data){
                if (data.errorCode == "0") {
                    localStorage.setItem("orderList", JSON.stringify(data));
                    location.href="replace-order-03.html"
                } else {
                    layer.msg(data.tip, {icon: 6,time:1500});
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){}
        });

    }
